using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class StudentCourses : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadClassList();
            btnViewInfo.Enabled = true;
        }
    }
    protected void Select_Click(object sender, EventArgs e)
    {
        
        if (LoadTitleList())
        {
            btnViewInfo.Enabled = true;
        }
        else
        {
            btnViewInfo.Enabled = false;
        }
    }
    protected void btnViewInfo_Click(object sender, EventArgs e)
    {
        LoadTitleInfo();
    }
    private void LoadClassList()
    {
        //Declare objects
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;


        string query = @"SELECT  course_Name AS 'COURSE' FROM Course";

        string connectionString = ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;

        //Initialize connection
        conn = new SqlConnection(connectionString);

        //Create command
        comm = new SqlCommand(query, conn);

        //Database code Try/Catch block
        try
        {
            //Open connection
            conn.Open();

            //Execute command
            reader = comm.ExecuteReader();

            //Populate genre list
            
            ddlClasses.DataSource = reader;
            ddlClasses.DataValueField = "COURSE";
            ddlClasses.DataTextField = "COURSE";
            ddlClasses.DataBind();

            //CLose reader
            reader.Close();
        }
        catch
        {//Display error message
            lblErrorMessage.Text = "Error loading list of genres";
        }
        finally
        {
            //Close connection
            conn.Close();
        }

        txtboxClassName.Text = "";
        txtboxRoomNumber.Text = "";
        txtboxInstructor.Text = "";
        txtboxCredits.Text = "";
        txtboxMinimumStudents.Text = "";
        txtboxMaximumStudents.Text = "";
    }
    private bool LoadTitleList()
    {
        //Declare objects
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        bool success = false;

        string query = @"SELECT * FROM Course";

        string connectionString =
            ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;

        //Initialize connection
        conn = new SqlConnection(connectionString);

        //Create command
        comm = new SqlCommand(query, conn);

        //Add parameter value
        comm.Parameters.AddWithValue("@genre", ddlClasses.SelectedValue);

        //Database code Try/Catch block
        try
        {
            //Open connection
            conn.Open();

            //Execute command
            reader = comm.ExecuteReader();

            if (reader.HasRows)
            {
                //Populate genre list
                ddlClasses.DataSource = reader;
                ddlClasses.DataSource = reader;
                ddlClasses.DataValueField = "Course";
                ddlClasses.DataTextField = "Course";
                ddlClasses.DataBind();

                //CLose reader
                reader.Close();
                success = true;
            }
            else
            {
                //Populate genre list
                ddlClasses.Items.Clear();
                ddlClasses.Items.Insert(0, new ListItem("No Classes"));

                //Close the reader
                reader.Close();
            }
        }
        catch
        {
            //Display error message
            lblErrorMessage.Text = "Error loading classes 2";
        }
        finally
        {
            //Close connection
            conn.Close();
        }
        txtboxClassName.Text = "";
        txtboxInstructor.Text = "";
        txtboxRoomNumber.Text = "";
        txtboxCredits.Text = "";
        txtboxMinimumStudents.Text = "";
        txtboxMaximumStudents.Text = "";
        return success;

    }

    private void LoadTitleInfo()
    {
        //Declare objects
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;

        string query =
            "SELECT " +
                "course_Name AS 'COURSE NAME', " +
                "room_Num AS 'ROOM NUMBER', " +
                "credits AS 'CREDITS', " +
                "min_Student AS 'MINIMUM STUDENTS', " +
                "max_Student AS 'MAXIMUM STUDENTS', " +
                "first_Name as 'FIRST NAME'," +
                "last_Name as 'LAST NAME'" +
            "FROM " +
                "Course, Instructor WHERE course_ID = @classes AND Course.instructor_ID = Instructor.instructor_ID";

        string connectionString =
            ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;

        //Initialize connection
        conn = new SqlConnection(connectionString);

        //Create command
        comm = new SqlCommand(query, conn);

        //Add parameter value
        comm.Parameters.AddWithValue("@classes", ddlClasses.SelectedValue);

        //Database code Try/Catch block
        try
        {
            //Open connection
            conn.Open();

            //Execute command
            reader = comm.ExecuteReader();

            if (reader.Read())
            {
                txtboxClassName.Text = reader["COURSE NAME"].ToString();
                txtboxRoomNumber.Text = reader["ROOM NUMBER"].ToString();
                txtboxInstructor.Text = reader["FIRST NAME"].ToString() + " " + reader["LAST NAME"].ToString();
                txtboxCredits.Text = reader["CREDITS"].ToString();
                txtboxMinimumStudents.Text = reader["MINIMUM STUDENTS"].ToString();
                txtboxMaximumStudents.Text = reader["MAXIMUM STUDENTS"].ToString();
            }
            else
            {
                lblErrorClasses.Text = "Error loading class information";
            }
            //Close reader
            reader.Close();
        }
        catch
        {
            //Display error message
            lblErrorMessage.Text = "Error loading classes";
        }
        finally
        {
            //Close connection
            conn.Close();
        }
    }
}


